﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace BeerManagementSystem
{
    public partial class Beerbill : Form
    {
        private Dictionary<User, string> _adresses;
        public Beerbill(Dictionary<User,string> adresses)
        {
            InitializeComponent();
            _adresses = adresses;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            try
            {
                MailAddress fromAddress = new MailAddress("beerbill@schmidtwoch.com", "BMS");

                // You can specify the host name or ipaddress of your server
                // Default in IIS will be localhost 
                smtpClient.Host = "smtp.schmidtwoch.com";
                smtpClient.Credentials = new System.Net.NetworkCredential("m02753e5", "PghNUTwd9EkG5EwD");

                //Default port will be 25
                smtpClient.Port = 25;

                //From address will be given as a MailAddress Object
                message.From = fromAddress;
                message.Subject = "Beerbill";

                //Body can be Html or text format
                //Specify true if it  is html message
                message.IsBodyHtml = false;

                
                foreach (var adress in _adresses)
                {
                    message.To.Clear();
                    message.Body = "";
                    string fname = adress.Key.FirstName;
                    string lname = adress.Key.LastName;
                    fname = fname.Replace(" ", "-");
                     // To address of MailAddress
                    message.To.Add(adress.Value);
                    // Message body content
                    message.Body = messageRichTextBox.Text + "http://www.schmidtwoch.com/profil/" + fname + "-" + lname;
                    // Send SMTP mail
                    smtpClient.Send(message );
                    
                }
                
                

                statusLabel.Text = "Email successfully sent.";
                LogWriter.Write("Beerbill erfolgreich versendet", LogWriter.LogLevel.Status, this);
            }
            catch (SmtpException ex)
            {
                LogWriter.Write("SendBeerbill: " + ex.InnerException,LogWriter.LogLevel.Error, this);
            }
        }
    }
}
